package core;

import java.io.File;
import java.io.FileNotFoundException;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class Graph
{
    public Vertex []vertexes;
    public Edge [][]edges;

    public static void main(String []args)
    {
        Graph graph=new Graph(26);
        graph.printVertexes();
        //graph.printEdges();
        graph.printLength();
    }

    public Graph(int vertexSize)
    {
       vertexes=new Vertex[vertexSize];
       edges=new Edge[vertexSize][vertexSize];

       setVertexes();
       setEdges();
       setAdjacent();
    }
    //设置点数组
    public void setVertexes()
    {
        for (int i=0;i<vertexes.length;i++)
        {
            char name=(char)('A'+i);
            int opoX=0;
            int opoY=0;
            vertexes[i]=new Vertex(name,opoX,opoY);
        }
    }
    public void setAdjacent()
    {
        for (int i=0;i<vertexes.length;i++)
        {
            for (int j=0;j<vertexes.length;j++)
            {
                if (i==j)
                    continue;

                Edge edge=edges[i][j];
                if (edge!=null)
                    vertexes[i].addAdj(vertexes[j]);
            }
        }
    }
    //设置边数组
    public void setEdges()
    {
        setDist();
        setCondition();
        setBusTime();
    }

    public void setDist()
    {
        File file=new File("./info/dist.txt");
        Scanner input=null;
        try
        {
            input=new Scanner(file);
            while (true)
            {
                String item=input.nextLine();
                char []itemArr=item.toCharArray();
                char start=itemArr[0];
                char end=itemArr[2];
                double dist=getDouble(item.substring(4,item.length()));

                edges[start-'A'][end-'A']=new Edge(start,end);
                edges[start-'A'][end-'A'].setDist(dist);
                edges[start-'A'][end-'A'].setWalkTime();


                edges[end-'A'][start-'A']=new Edge(end,start);
                edges[end-'A'][start-'A'].setDist(dist);
                edges[end-'A'][start-'A'].setWalkTime();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchElementException e)
        {
            //e.printStackTrace();
        }
        finally
        {
            input.close();
        }
    }
    public void setBusTime()
    {
        File file=new File("./info/busTime.txt");
        Scanner input=null;
        try
        {
            input=new Scanner(file);
            while (true)
            {
                String item=input.nextLine();
                char []itemArr=item.toCharArray();
                char start=itemArr[0];
                char end=itemArr[2];
                double busTime=getDouble(item.substring(4,item.length()));

                if (edges[start-'A'][end-'A']==null)
                {
                    edges[start-'A'][end-'A']=vrBusEdge(start,end,busTime);
                    edges[end-'A'][start-'A']=vrBusEdge(end,start,busTime);
                }
                else
                {
                    edges[start-'A'][end-'A'].setBusTime(busTime);
                    edges[start-'A'][end-'A'].setMode(true,false,true);

                    edges[end-'A'][start-'A'].setBusTime(busTime);
                    edges[end-'A'][start-'A'].setMode(true,false,true);
                }
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchElementException e)
        {
            //e.printStackTrace();
        }
        finally
        {
            input.close();
        }
    }
    public Edge vrBusEdge(char start,char end,double busTime)
    {
        Edge vrBusEdge=new Edge(start,end);
        vrBusEdge.setBusTime(busTime);
        vrBusEdge.setDist(-1);
        vrBusEdge.setMode(false,false,true);

        return vrBusEdge;
    }
    public void setCondition()
    {
        File file=new File("./info/condition.txt");
        Scanner input=null;
        try
        {
            input=new Scanner(file);
            while (true)
            {
                String item=input.nextLine();
                char []itemArr=item.toCharArray();
                char start=itemArr[0];
                char end=itemArr[2];
                int mode=itemArr[4]-'0';
                switch (mode)
                {
                    case 0:
                    {
                        edges[end-'A'][start-'A'].setDrive(false);
                        edges[end-'A'][start-'A'].setBus(false);
                        //edges[end-'A'][start-'A']=null;
                        break;
                    }
                    case 1:
                    {
                        edges[start-'A'][end-'A'].setMode(false,true,false);
                        if ( edges[end-'A'][start-'A']!=null)
                            edges[end-'A'][start-'A'].setMode(false,true,false);
                        break;
                    }
                    case 2:
                    {
                        edges[start-'A'][end-'A'].setMode(true,false,false);
                        if ( edges[end-'A'][start-'A']!=null)
                            edges[end-'A'][start-'A'].setMode(true,false,false);
                        break;
                    }
                }
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchElementException e)
        {
            //e.printStackTrace();
        }
        finally
        {
            input.close();
        }
    }
    public static double getDouble(String str)
    {
        if (str.length()==0)
            return -1;

        char []strArr=str.toCharArray();
        double value=0;
        int index=str.indexOf(".");
        if(index<0)
            index=str.length();

        for (int i=index-1;i>=0;i--)
            value+=(strArr[i]-'0')*Math.pow(10,index-i-1);

        for (int i=index+1;i<str.length();i++)
            value+=(strArr[i]-'0')*Math.pow(10,index-i);

        return value;
    }
    public void printEdges()
    {
        for (int i=0;i<edges.length;i++)
        {
            for (int j=0;j<edges[0].length;j++)
            {
                Edge tmp=edges[i][j];
                if (tmp!=null)
                    System.out.println(tmp.start+" "+tmp.end+" dist:"+tmp.dist+" walk:"+tmp.walk+" drive:"+tmp.drive+" bus:"+tmp.bus);
            }
        }

    }
    public void printVertexes()
    {
        for (int i=0;i<vertexes.length;i++)
        {
            Vertex vertex=vertexes[i];
            System.out.print(vertex.getName()+":");
            vertex.printAdjacent();
            System.out.println();
        }
    }
    public void printLength()
    {
        for (int i=0;i<edges.length;i++)
        {
            for (int j=0;j<edges[0].length;j++)
            {
                Edge tmp=edges[i][j];
                if (tmp!=null)
                    System.out.println(tmp.start+" "+tmp.end+" dist:"+tmp.dist+" walkTime:"+tmp.walkTime+" busTime:"+tmp.getBusTime());
            }
        }

    }
}
